from random import randint
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select
drive = webdriver.Firefox()
drive.get('http://localhost/jxc/index.asp')
# ---------------(1)----------------
ipts = drive.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
sleep(2)
ipts[2].click()

# ---------------(2)----------------
# 系统设置
drive.switch_to.frame('left')
xtsz_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
drive.find_element_by_css_selector(xtsz_css).click()
sleep(1)
# 单位
dw_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
drive.find_element_by_css_selector(dw_css).click()
# 全选 删除 确认
drive.switch_to.default_content()
drive.switch_to.frame('right')
drive.find_element_by_id('chkall').click()
sc_css = 'input.button:nth-child(2)'
drive.find_element_by_css_selector(sc_css).click()
sleep(1)
drive.switch_to.alert.accept()

# ---------------(3)----------------
drive.find_element_by_class_name('button').click()
tjdw_css = '.toptable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > input:nth-child(1)'
drive.find_element_by_css_selector(tjdw_css).send_keys('kg')
sleep(1)
drive.find_element_by_name('submit').click()
sleep(1)
drive.switch_to.alert.accept()

# ---------------(4)----------------
drive.switch_to.default_content()
drive.switch_to.frame('left')

cpdl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
drive.find_element_by_css_selector(cpdl_css).click()
drive.switch_to.default_content()
drive.switch_to.frame('right')
tjdl_css = 'input.button:nth-child(1)'
drive.find_element_by_css_selector(tjdl_css).click()
sleep(1)
drive.find_element_by_name('bigclass').send_keys('其他')
sleep(2)
drive.find_element_by_name('submit').click()
sleep(1)
drive.switch_to.alert.accept()

# ---------------(5)----------------
drive.switch_to.default_content()
drive.switch_to.frame('left')
sleep(1)
cpxl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
drive.find_element_by_css_selector(cpxl_css).click()
drive.switch_to.default_content()
drive.switch_to.frame('right')
tjxl_css = 'input.button:nth-child(1)'
drive.find_element_by_css_selector(tjxl_css).click()

ssdl_css = '.toptable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > select:nth-child(1)'
ssdl = drive.find_element_by_css_selector(ssdl_css)
ssdl_lst = Select(ssdl)
lst_count = len(ssdl_lst.options)
rand = randint(0, lst_count - 1)
ssdl_lst.select_by_index(rand)
sleep(2)
drive.find_element_by_name('smallclass').send_keys('调味品')
sleep(1)
drive.find_element_by_name('submit').click()
drive.switch_to.alert.accept()
sleep(2)

# ---------------(5)----------------
drive.quit()